/*
 * ServerUI.java
 *
 * Created on July 13, 2007, 10:16 AM
 */

package demo.tuio;


import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.WindowConstants;

/**
 *
 * @author  gestalt
 */
public class ServerUI extends javax.swing.JFrame
{
    
    private final Image icon;        

    /** Creates new form ServerUI */
    public ServerUI()
    {
        {
            Image image = null;
            try {
                image = ImageIO.read(ServerUI.class.getResource("/de/telekom/Icon.png"));
            }
            catch (IOException ioe) {
            }
            icon = image;
        }   
        initComponents();
    }
    
    private void exit()
    {
        System.exit(0);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        frameSubtractionButtonGroup = new javax.swing.ButtonGroup();
        mainPanel = new javax.swing.JPanel();
        jSeparatorOne = new javax.swing.JSeparator();
        jInetSocketAddressLabel = new javax.swing.JLabel();
        jHostLabel = new javax.swing.JLabel();
        jHostTextField = new javax.swing.JTextField();
        jPortLabel = new javax.swing.JLabel();
        jPortSpinner = new javax.swing.JSpinner();
        jCameraSettingsLabel = new javax.swing.JLabel();
        jResolutionLabel = new javax.swing.JLabel();
        jFrameRateComboBox = new javax.swing.JComboBox();
        jSeparatorTwo = new javax.swing.JSeparator();
        jFlipLabel = new javax.swing.JLabel();
        jHorizontalCheckBox = new javax.swing.JCheckBox();
        jVerticalCheckBox = new javax.swing.JCheckBox();
        jImageProcessingLabel = new javax.swing.JLabel();
        jFrameSubtractionLabel = new javax.swing.JLabel();
        jNeverRadioButton = new javax.swing.JRadioButton();
        jOnceRadioButton = new javax.swing.JRadioButton();
        jAlwaysRadioButton = new javax.swing.JRadioButton();
        jThresholdLabel = new javax.swing.JLabel();
        jThresholdSlider = new javax.swing.JSlider();
        jSeparatorThree = new javax.swing.JSeparator();
        jImageViewToggleButton = new javax.swing.JToggleButton();
        jComboBox1 = new javax.swing.JComboBox();
        jFrameRateLabel = new javax.swing.JLabel();
        jCommandPanel = new javax.swing.JPanel();
        jCancelButton = new javax.swing.JButton();
        jOKCancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Multi-T-ouch: TUIO Server");
        setIconImage(icon);
        setLocationByPlatform(true);

        jInetSocketAddressLabel.setFont(jInetSocketAddressLabel.getFont().deriveFont(jInetSocketAddressLabel.getFont().getStyle() | java.awt.Font.BOLD, jInetSocketAddressLabel.getFont().getSize()+1));
        jInetSocketAddressLabel.setText("Internet Socket Address");

        jHostLabel.setText("Host:");

        jHostTextField.setText("localhost");

        jPortLabel.setText("Port:");

        jCameraSettingsLabel.setFont(jCameraSettingsLabel.getFont().deriveFont(jCameraSettingsLabel.getFont().getStyle() | java.awt.Font.BOLD, jCameraSettingsLabel.getFont().getSize()+1));
        jCameraSettingsLabel.setText("Camera Settings");

        jResolutionLabel.setText("Resolution:");

        jFrameRateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "  30" }));

        jFlipLabel.setText("Flip:");

        jHorizontalCheckBox.setText("horizontal");
        jHorizontalCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jVerticalCheckBox.setText("vertical");
        jVerticalCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jImageProcessingLabel.setFont(jImageProcessingLabel.getFont().deriveFont(jImageProcessingLabel.getFont().getStyle() | java.awt.Font.BOLD, jImageProcessingLabel.getFont().getSize()+1));
        jImageProcessingLabel.setText("Image Processing");

        jFrameSubtractionLabel.setText("Frame-Subtraction:");

        frameSubtractionButtonGroup.add(jNeverRadioButton);
        jNeverRadioButton.setText("never");
        jNeverRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        frameSubtractionButtonGroup.add(jOnceRadioButton);
        jOnceRadioButton.setSelected(true);
        jOnceRadioButton.setText("once");
        jOnceRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        frameSubtractionButtonGroup.add(jAlwaysRadioButton);
        jAlwaysRadioButton.setText("always");
        jAlwaysRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        jThresholdLabel.setText("Threshold:");

        jThresholdSlider.setMajorTickSpacing(50);
        jThresholdSlider.setMaximum(255);
        jThresholdSlider.setMinorTickSpacing(5);
        jThresholdSlider.setPaintLabels(true);
        jThresholdSlider.setPaintTicks(true);

        jImageViewToggleButton.setText("View Image");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1024 x 768" }));

        jFrameRateLabel.setText("Frame-Rate:");

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jSeparatorTwo, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jSeparatorOne, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jHostLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jHostTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jPortLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPortSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12))
                            .addComponent(jInetSocketAddressLabel)
                            .addComponent(jCameraSettingsLabel)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jFlipLabel)
                                    .addComponent(jResolutionLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addComponent(jHorizontalCheckBox)
                                        .addGap(18, 18, 18)
                                        .addComponent(jVerticalCheckBox))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                        .addComponent(jComboBox1, 0, 158, Short.MAX_VALUE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jFrameRateLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jFrameRateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(14, 14, 14)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addComponent(jImageProcessingLabel)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addComponent(jThresholdLabel)
                                        .addGap(18, 18, 18)
                                        .addComponent(jThresholdSlider, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE))
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addComponent(jFrameSubtractionLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
                                        .addComponent(jNeverRadioButton)
                                        .addGap(18, 18, 18)
                                        .addComponent(jOnceRadioButton)
                                        .addGap(18, 18, 18)
                                        .addComponent(jAlwaysRadioButton)))
                                .addGap(10, 10, 10)))
                        .addGap(0, 0, 0))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jImageViewToggleButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                            .addComponent(jSeparatorThree, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jInetSocketAddressLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jHostLabel)
                    .addComponent(jPortSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPortLabel)
                    .addComponent(jHostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparatorOne, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jCameraSettingsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jResolutionLabel)
                    .addComponent(jFrameRateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFrameRateLabel)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jFlipLabel)
                    .addComponent(jHorizontalCheckBox)
                    .addComponent(jVerticalCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparatorTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jImageProcessingLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jFrameSubtractionLabel)
                    .addComponent(jAlwaysRadioButton)
                    .addComponent(jOnceRadioButton)
                    .addComponent(jNeverRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jThresholdLabel)
                    .addComponent(jThresholdSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparatorThree, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jImageViewToggleButton)
                .addContainerGap())
        );

        getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

        jCancelButton.setText("Cancel");

        jOKCancelButton.setText("OK");

        javax.swing.GroupLayout jCommandPanelLayout = new javax.swing.GroupLayout(jCommandPanel);
        jCommandPanel.setLayout(jCommandPanelLayout);
        jCommandPanelLayout.setHorizontalGroup(
            jCommandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jCommandPanelLayout.createSequentialGroup()
                .addContainerGap(226, Short.MAX_VALUE)
                .addComponent(jOKCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jCommandPanelLayout.setVerticalGroup(
            jCommandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jCommandPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jCommandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCancelButton)
                    .addComponent(jOKCancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jCommandPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new ServerUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup frameSubtractionButtonGroup;
    private javax.swing.JRadioButton jAlwaysRadioButton;
    private javax.swing.JLabel jCameraSettingsLabel;
    private javax.swing.JButton jCancelButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JPanel jCommandPanel;
    private javax.swing.JLabel jFlipLabel;
    private javax.swing.JComboBox jFrameRateComboBox;
    private javax.swing.JLabel jFrameRateLabel;
    private javax.swing.JLabel jFrameSubtractionLabel;
    private javax.swing.JCheckBox jHorizontalCheckBox;
    private javax.swing.JLabel jHostLabel;
    private javax.swing.JTextField jHostTextField;
    private javax.swing.JLabel jImageProcessingLabel;
    private javax.swing.JToggleButton jImageViewToggleButton;
    private javax.swing.JLabel jInetSocketAddressLabel;
    private javax.swing.JRadioButton jNeverRadioButton;
    private javax.swing.JButton jOKCancelButton;
    private javax.swing.JRadioButton jOnceRadioButton;
    private javax.swing.JLabel jPortLabel;
    private javax.swing.JSpinner jPortSpinner;
    private javax.swing.JLabel jResolutionLabel;
    private javax.swing.JSeparator jSeparatorOne;
    private javax.swing.JSeparator jSeparatorThree;
    private javax.swing.JSeparator jSeparatorTwo;
    private javax.swing.JLabel jThresholdLabel;
    private javax.swing.JSlider jThresholdSlider;
    private javax.swing.JCheckBox jVerticalCheckBox;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
    
    // <editor-fold defaultstate="collapsed" desc=" System Tray ">
        
    private boolean systemTray()
    {
        if (icon == null || !SystemTray.isSupported()) return false;
        
        final SystemTray tray = SystemTray.getSystemTray();


        final ActionListener exitListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                exit();
}
        };

        final PopupMenu popup = new PopupMenu();

        //final CheckboxMenuItem cameraItem = new CheckboxMenuItem("Camera");
        final MenuItem stopItem = new MenuItem("Stop");
        stopItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                exit();
            }
        });

        final MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(exitListener);

        //popup.add(cameraItem);
        //popup.addSeparator();
        popup.add(exitItem);

        final TrayIcon trayIcon = new TrayIcon(icon, "MultiTouch: TUIO Server", popup);
        trayIcon.setImageAutoSize(true);

        try {
            tray.add(trayIcon);
        }
        catch (AWTException e) {
            return false;
        }

        return true;
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc=" Floating Window ">
    private void floatingWindow()
    {
        final JDialog jDialog = new JDialog();
        jDialog.setLocationByPlatform(true);
        jDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        jDialog.setTitle("Multi-T-ouch: TUIO Server");

        jDialog.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e)
            {
                exit();
            }
        });

        //final JToggleButton jCameraButton = new JToggleButton("Camera");
        final JButton jStopButton = new JButton("Stop");
        jStopButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                exit();
            }
        });

        jDialog.getContentPane().setLayout(new GridLayout(1, 1));
        //jDialog.getContentPane().setLayout(new GridLayout(2, 1));
        //jDialog.getContentPane().add(jCameraButton);
        jDialog.getContentPane().add(jStopButton);

        jDialog.setPreferredSize(new Dimension(240, 120));
        jDialog.pack();
        jDialog.setResizable(false);
        jDialog.setVisible(true);
    }

    // </editor-fold>    
    
}
